package znet

import (
	"fmt"
	"strconv"
	ziface "zinx/Ziface"
)



type MsgHandler struct {

	//根据Msgid管理路由模块
	Apis map[uint32] ziface.IRouter
}



//执行对应的方法
func(msghd *MsgHandler)DoMsgHandler(request ziface.IRuquest){
	handler,ok:=msghd.Apis[request.GetMsgId()]
	if !ok{
		fmt.Println("api msgid=",request.GetMsgId(),"is not FOUND! Need Register")
		return
	}
	handler.PreHandler(request)
	handler.Handler(request)
	handler.PostHandler(request)
}


//为消息添加对应的处理逻辑
func(msghd *MsgHandler)AddRouter(msgID uint32,router ziface.IRouter){
	//如果当前msgid已经被注册
	if _,ok :=msghd.Apis[msgID];ok {
		//如果已经被注册
		panic("repeat api,msgID=" + strconv.Itoa(int(msgID)))
	}
	msghd.Apis[msgID]=router
	fmt.Println("Add api msgID=",msgID,"success!")
}

func NewMsgHandler() *MsgHandler {
	return &MsgHandler{
		Apis: make(map[uint32] ziface.IRouter),
	}
}